package com.wisdomparking.control.app;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.wisdomparking.bean.VehicleInfo;
import com.wisdomparking.config.mvc.BaseController;
import com.wisdomparking.config.mvc.Result;
import com.wisdomparking.config.mvc.ResultGenerator;
import com.wisdomparking.constant.Constants;
import com.wisdomparking.service.VehicleInfoService;
import com.wisdomparking.tools.ToolFile;
import com.wisdomparking.tools.ToolValid;

/**
 * 车辆中心
 */
@RestController
@RequestMapping("/app/vehicleCenter")
public class VehicleInfoController extends BaseController {

	@Autowired
	VehicleInfoService vehicleInfoService;

	/**
	 * 获取用户车辆信息
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/getVehicleInfo")
	public Result<VehicleInfo> getVehicleInfo(Integer vehicleId) {
		return ResultGenerator.genSuccessResult(vehicleInfoService.getVehicleInfo(vehicleId));
	}

	/**
	 * 获取用户车辆信息列表
	 * 
	 * @param userId
	 * @return
	 */
	@RequestMapping("/getVehicleInfoList")
	public Result<List<VehicleInfo>> getVehicleInfoList(String userId) {
		Map<String, Object> map = new HashMap<>();
		map.put("userId", userId);

		return ResultGenerator.genSuccessResult(vehicleInfoService.getVehicleInfoList(map));
	}

	/**
	 * 添加车辆
	 * 
	 * @param plateNumber
	 * @return
	 */
	@RequestMapping("/addVehicleInfo")
	public Result<VehicleInfo> addVehicleInfo(Integer userId, String plateNumber) {
		
		VehicleInfo vehicleInfo = null;
		
		if (ToolValid.isNotEmpty(plateNumber) && ToolValid.isNotEmpty(userId)) {
			VehicleInfo old_vehicleInfo = vehicleInfoService.getVehicleInfoByPlateNumber(plateNumber);

			if (ToolValid.isNull(old_vehicleInfo)) {
				vehicleInfo = new VehicleInfo();
				vehicleInfo.setUserId(userId);
				vehicleInfo.setPlateNumber(plateNumber);
				vehicleInfo.setCreateTime(new Date());
				vehicleInfo.setActivatedState(true);
				
				Map<String, Object> map = new HashMap<>();
				map.put("userId", userId);
				
				List<VehicleInfo> list = vehicleInfoService.getVehicleInfoList(map);
				
				if(ToolValid.isEmpty(list)) {
					vehicleInfo.setIsDefault(true);
				}
				else {
					vehicleInfo.setIsDefault(false);
				}
				
				vehicleInfoService.addVehicleInfo(vehicleInfo, true);
			}

		}

		return ResultGenerator.genSuccessResult(vehicleInfo);
	}

	/**
	 * 添加发动机号
	 * 
	 * @param engineNumber
	 * @return
	 */
	@RequestMapping("/addEngineNumber")
	public Result<VehicleInfo> addEngineNumber(Integer vehicleId, String engineNumber) {
		VehicleInfo vehicleInfo = null;

		if (ToolValid.isNotEmpty(vehicleId) && ToolValid.isNotEmpty(engineNumber)) {
			vehicleInfo = vehicleInfoService.getVehicleInfo(vehicleId);
			if (ToolValid.isNotNull(vehicleInfo)) {
				vehicleInfo.setEngineNumber(engineNumber);
				vehicleInfoService.updateVehicleInfo(vehicleInfo, true);
			}
		}

		return ResultGenerator.genSuccessResult(vehicleInfo);
	}

	/**
	 * 上传行驶证
	 * 
	 * @param engineNumber
	 * @return
	 */
	@RequestMapping("/uploadVehicleLicense")
	public Result<VehicleInfo> uploadVehicleLicense(Integer vehicleId, MultipartFile file, HttpServletRequest request) {
		VehicleInfo vehicleInfo = null;
		
		if (ToolValid.isNotEmpty(vehicleId) && ToolValid.isNotNull(file)) {
			vehicleInfo = vehicleInfoService.getVehicleInfo(vehicleId);
			if (ToolValid.isNotNull(vehicleInfo)) {
				try {
					String folder = "imgupload" + File.separator + "vehicleInfo" + File.separator;
					String fileName = file.getOriginalFilename();
					String filePath = request.getSession().getServletContext().getRealPath(folder);

					ToolFile.uploadFile(file.getBytes(), filePath, vehicleId + Constants.SPLIT_STRIPING + fileName);
					String src = folder + vehicleId + Constants.SPLIT_STRIPING + fileName;

					vehicleInfo.setVehicleLicensePic(src);
					vehicleInfo.setValidatingState(true);

					vehicleInfoService.updateVehicleInfo(vehicleInfo, true);
				} 
				catch (Exception e) {
					e.printStackTrace();
				}

			}
		}

		return ResultGenerator.genSuccessResult(vehicleInfo);
	}
	
	/**
	 * 设置默认使用车辆
	 * @param vehicleId
	 * @return
	 */
	@RequestMapping("/configDefaultVehicle")
	public Result<VehicleInfo> configDefaultVehicle(Integer vehicleId) {
		VehicleInfo vehicleInfo = null;

		if (ToolValid.isNotEmpty(vehicleId)) {
			vehicleInfo = vehicleInfoService.getVehicleInfo(vehicleId);
			
			if (ToolValid.isNotNull(vehicleInfo)) {
				Map<String, Object> map = new HashMap<>();
				
				if(ToolValid.isNotEmpty(vehicleInfo.getUserId())) {
					map.put("userId", vehicleInfo.getUserId());
					List<VehicleInfo> list = vehicleInfoService.getVehicleInfoList(map);
					
					if(ToolValid.isNotEmpty(list)) {
						for(VehicleInfo v: list) {
							if(ToolValid.isNotNull(v)) {
								if(v.getId()==vehicleId) {
									v.setIsDefault(true);
								}
								else {
									v.setIsDefault(false);
								}
								
								vehicleInfoService.updateVehicleInfo(v, false);
							}
						}
					}
				}
				
			}
		}

		return ResultGenerator.genSuccessResult(vehicleInfo);
	}

}
